<?php

class IndexController extends Zend_Controller_Action
{
    /**
     * Url for page
     * @var string
     */
    private $pageUrl = 'index';
    
    /**
     * Current page
     * @var Application_Model_Pages
     */
    private $page = null;
        
    /**
     * (non-PHPdoc)
     * @see Controller/Zend_Controller_Action::init()
     */
    public function init()
    {
    	$this->pageUrl = $this->getRequest()->getActionName();
        $this->model = new Application_Model_Pages();
    	$this->itemsLoader = $this->_helper->itemsLoader($this->model);
    	
		try
		{
	    	$this->page = $this->itemsLoader->loadPage($this->pageUrl);
    	}
		catch(Exception $e)
    	{
			throw new Zend_Controller_Exception($e->getMessage());
    	}
    }
    
    /**
     * Proxy for undefined methods.  Default behavior is to throw an
     * exception on undefined methods, however this function can be
     * overridden to implement magic (dynamic) actions, or provide run-time
     * dispatching.
     *
     * @param  string $methodName
     * @param  array $args
     * @return void
     * @throws Zend_Controller_Action_Exception
     */
    public function __call($methodName, $args)
    {
    	$this->getRequest()->setActionName('index');
    	$this->indexAction();
    }

    public function indexAction()
    {
        // action body
    }
    
    public function postDispatch()
    {
		$this->view = $this->itemsLoader->setPageParameters($this->view, $this->page);
		$this->view->page = $this->page;
    }
}